package com.lqk.java2;

import com.lqk.java1.Person;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;

/**
 * @author liqiankun
 * @date 2022/11/10 17:28
 * @description 获取运行时类的方法结构
 **/
public class MethodTest {
    @Test
    public void test1(){
        Class clazz = Person.class;
        /**
         * getMethods(): 获取当前运行时类及其所有父类中声明为public权限的方法
         */
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("=====================================");
        /**
         * getDeclaredMethods(): 获取当前运行时类中声明的所有方法.(不包含父类中声明的方法)
         */
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }

    /**
     * 注解: @Xxx
     * 权限修饰符 返回值类型 方法名(参数类型1 形参名1, 参数类型2 形参名2, ...) throws XxxException{}
     */
    @Test
    public void test2(){
        Class clazz = Person.class;
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            // 获取方法声明的注解
            Annotation[] annotations = declaredMethod.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }
            // 获取权限修饰符
            int modifiers = declaredMethod.getModifiers();
            System.out.print(Modifier.toString(modifiers)+"\t");
            // 获取返回值类型
            Class<?> returnType = declaredMethod.getReturnType();
            System.out.print(returnType+"\t");
            // 获取方法名
            String name = declaredMethod.getName();
            System.out.print(name + "(");
            // 获取形参列表
            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            Parameter[] parameters = declaredMethod.getParameters();
            if (parameterTypes.length>0 ){
                for (int i = 0; i <parameters.length; i++) {
                    if(i == parameters.length -1){
                        System.out.print(parameterTypes[i].getTypeName() + " " + parameters[i].getName());
                        break;
                    }
                    System.out.print(parameterTypes[i].getTypeName() + " " + parameters[i].getName() + " ,");
                }
            }
            System.out.print(")");
            // 抛出异常
            Class<?>[] exceptionTypes = declaredMethod.getExceptionTypes();
            if (exceptionTypes.length > 0){
                System.out.print("throws ");
                for (int i = 0; i < exceptionTypes.length; i++) {
                    if(i == exceptionTypes.length -1){
                        System.out.print(exceptionTypes[i].getName());
                        break;
                    }
                    System.out.print(exceptionTypes[i].getName() + " ,");
                }
            }
            System.out.println();
        }
    }
}
